//
//  UIKToastView.swift
//  UIKToastView
//
//  Created by Fineland on 2021/3/18.
//

import UIKit

enum UIKToastViewType: String {
    
    case info = "system_alert_info"
    case success = "system_alert_scuccess"
    case unknow
}

// MARK: -

class UIKToastView {
    
    //单例
    static let shared = UIKToastView()
    //背景
    private var mskView: UIView!
    private var bgView: UIView!
    //内容
    private var contentLabel: UILabel!

    //闭包
    typealias ReturnClosure = (_ finished: Bool) -> Void
    //回调
    private var returnClosure: ReturnClosure?
    
    class func showText(_ text: String, type: UIKToastViewType = .info, closure: @escaping ReturnClosure) {
        
        //单例对象
        let tempObj = UIKToastView.shared
        tempObj.returnClosure = closure
        //显示内容
        UIKToastView.showText(text, type: type)
    }
    
    class func showText(_ text: String, type: UIKToastViewType = .info) {
        
        //列表是否大于0？
        let tempArray = UIApplication.shared.windows
        if tempArray.count < 1 {
            return
        }
        //单例对象
        let tempObj = UIKToastView.shared
        //添加
        tempArray.first!.addSubview(tempObj.bgView)
        //提示内容
        let tempVal = tempObj.attrText(text, with: type.rawValue)
        tempObj.resizeText(tempVal)
        //动画
        tempObj.animating()
    }
    
    private init() {
        
        //背景
        bgView = UIView(frame: CGRect(origin: CGPoint.zero, size: UIScreen.main.bounds.size))
        bgView.isHidden = true
        //遮罩层
        let wVal = ceil(0.6 * bgView.frame.size.width)
        mskView = UIView(frame: CGRect(x: 0.5 * (bgView.frame.size.width - wVal) , y: 0.0, width: wVal, height: 0.0/*计算调整*/))
        mskView.backgroundColor = UIColor.black
        mskView.alpha = 0.0
        mskView.layer.cornerRadius = 4.0
        mskView.clipsToBounds = true
        //内容
        let xOffset: CGFloat = 10.0
        let xVal = mskView.frame.origin.x + xOffset
        contentLabel = UILabel(frame: CGRect(x: xVal, y: 0.0, width: wVal - 2.0 * xOffset, height: 0.0/*计算调整*/))
        contentLabel.font = UIFont.boldSystemFont(ofSize: 14.0)
        contentLabel.textAlignment = .center
        contentLabel.textColor = UIColor.white
        contentLabel.alpha = 0.0
        contentLabel.numberOfLines = 0
        //添加
        bgView.addSubview(mskView)
        bgView.addSubview(contentLabel)
    }
    
    private func resizeText(_ attrText: NSAttributedString) -> Void {
        
        //内容
        contentLabel.attributedText = attrText
        //RECT值
        var rect1 = contentLabel.frame
        var rect2 = mskView.frame
        let yOffset: CGFloat = 20.0
        //计算高度
        let szContent = contentLabel.sizeThatFits(CGSize(width: rect1.width, height: bgView.frame.size.height))
        rect1.origin.y = 0.5 * (bgView.frame.size.height - szContent.height)
        rect1.size.height = szContent.height
        rect2.origin.y = rect1.origin.y - yOffset
        rect2.size.height = 2.0 * yOffset + szContent.height
        //调整
        mskView.frame = rect2
        contentLabel.frame = rect1
    }
    
    private func animating(_ duration: CFTimeInterval = 0.25, sleep: TimeInterval = 1.5) -> Void {
        
        //显示
        bgView.isHidden = false
        //动画方式处理
        UIView.animate(withDuration: duration, animations: {
            self.mskView.alpha = 0.8
            self.contentLabel.alpha = 1.0
            
        }) { (finished: Bool) in
            //等待时间
            _ = Timer.scheduledTimer(withTimeInterval: sleep, repeats: false) { (t: Timer) in
                //动画回到原始状态
                UIView.animate(withDuration: duration, animations: {
                    self.mskView.alpha = 0.0
                    self.contentLabel.alpha = 0.0
                                    
                }) { (finished: Bool) in
                    //隐藏
                    self.bgView.isHidden = true
                    self.bgView.tag = 0
                    self.bgView.removeFromSuperview()
                    //回调是否为空？
                    if let returnClosure = self.returnClosure {
                        returnClosure(true)
                    }
                }
                t.invalidate()
            }
        }
    }
    
    private func attrText(_ text: String, with image: String) -> NSAttributedString {
        
        //图片大小
        let szImg = CGSize(width: 35.0, height: 35.0)
        let attachment = NSTextAttachment()
        attachment.bounds = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: szImg)
        attachment.image = UIImage(named: image)
        //值
        let tempVal = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        tempVal.append(NSAttributedString(string: "\n\n" + text))
        //返回
        return tempVal
    }
}
